/*     */ package com.zimbra.cs.mailbox.calendar.tzfixup;
/*     */ 
/*     */ import com.zimbra.common.calendar.ICalTimeZone;
/*     */ import com.zimbra.common.calendar.ICalTimeZone.SimpleOnset;
/*     */ import com.zimbra.common.calendar.TimeZoneMap;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TimeZoneFixupRules
/*     */ {
/*     */   private List<Matcher> mMatchers;
/*     */   
/*     */   private static enum MatchBy
/*     */   {
/*  36 */     ANY,  TZID,  OFFSET,  RULES,  DATES;
/*     */     
/*     */     private MatchBy() {}
/*     */   }
/*     */   
/*     */   public static class Matcher { private TimeZoneFixupRules.MatchBy mMatchBy;
/*     */     private String mTZID;
/*     */     private long mStandardOffset;
/*     */     private long mDaylightOffset;
/*     */     private ICalTimeZone.SimpleOnset mStandardOnset;
/*     */     private ICalTimeZone.SimpleOnset mDaylightOnset;
/*     */     private ICalTimeZone mReplacementTZ;
/*     */     
/*  49 */     public Matcher(ICalTimeZone replacementTZ) { this.mMatchBy = TimeZoneFixupRules.MatchBy.ANY;
/*  50 */       this.mReplacementTZ = replacementTZ;
/*     */     }
/*     */     
/*     */     public Matcher(String tzid, ICalTimeZone replacementTZ)
/*     */     {
/*  55 */       this.mMatchBy = TimeZoneFixupRules.MatchBy.TZID;
/*  56 */       this.mTZID = (tzid != null ? tzid : "");
/*  57 */       this.mReplacementTZ = replacementTZ;
/*     */     }
/*     */     
/*     */     public Matcher(long offset, ICalTimeZone replacementTZ)
/*     */     {
/*  62 */       this.mMatchBy = TimeZoneFixupRules.MatchBy.OFFSET;
/*  63 */       this.mStandardOffset = (this.mDaylightOffset = offset * 60000L);
/*  64 */       this.mReplacementTZ = replacementTZ;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */     public Matcher(long stdOffsetMins, int stdMonth, int stdWeek, int stdDayOfWeek, long dstOffsetMins, int dstMonth, int dstWeek, int dstDayOfWeek, ICalTimeZone replacementTZ)
/*     */     {
/*  71 */       this.mMatchBy = TimeZoneFixupRules.MatchBy.RULES;
/*  72 */       this.mStandardOffset = (stdOffsetMins * 60000L);
/*     */       
/*  74 */       this.mStandardOnset = new ICalTimeZone.SimpleOnset(stdWeek, stdDayOfWeek, stdMonth, 0, 2, 0, 0, true);
/*     */       
/*  76 */       this.mDaylightOffset = (dstOffsetMins * 60000L);
/*  77 */       this.mDaylightOnset = new ICalTimeZone.SimpleOnset(dstWeek, dstDayOfWeek, dstMonth, 0, 2, 0, 0, true);
/*     */       
/*  79 */       this.mReplacementTZ = replacementTZ;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */     public Matcher(long stdOffsetMins, int stdMonth, int stdDayOfMonth, long dstOffsetMins, int dstMonth, int dstDayOfMonth, ICalTimeZone replacementTZ)
/*     */     {
/*  86 */       this.mMatchBy = TimeZoneFixupRules.MatchBy.DATES;
/*  87 */       this.mStandardOffset = (stdOffsetMins * 60000L);
/*     */       
/*  89 */       this.mStandardOnset = new ICalTimeZone.SimpleOnset(0, 0, stdMonth, stdDayOfMonth, 2, 0, 0, true);
/*     */       
/*  91 */       this.mDaylightOffset = (dstOffsetMins * 60000L);
/*  92 */       this.mDaylightOnset = new ICalTimeZone.SimpleOnset(0, 0, dstMonth, dstDayOfMonth, 2, 0, 0, true);
/*     */       
/*  94 */       this.mReplacementTZ = replacementTZ;
/*     */     }
/*     */     
/*     */     private static boolean sameOnset(ICalTimeZone.SimpleOnset os1, ICalTimeZone.SimpleOnset os2) {
/*  98 */       if (os1 == null)
/*  99 */         return os2 == null;
/* 100 */       if (os2 == null) {
/* 101 */         return os1 == null;
/*     */       }
/*     */       
/* 104 */       if (os1.getMonth() != os2.getMonth()) {
/* 105 */         return false;
/*     */       }
/* 107 */       int week1 = os1.getWeek();
/* 108 */       if (week1 != 0) {
/* 109 */         return (week1 == os2.getWeek()) && (os1.getDayOfWeek() == os2.getDayOfWeek());
/*     */       }
/* 111 */       return os1.getDayOfMonth() == os2.getDayOfMonth();
/*     */     }
/*     */     
/*     */ 
/* 115 */     public boolean isTouchOnly() { return this.mReplacementTZ == null; }
/* 116 */     public ICalTimeZone getReplacementTZ() { return this.mReplacementTZ; }
/*     */     
/*     */     public boolean matches(ICalTimeZone tz) {
/* 119 */       switch (TimeZoneFixupRules.1.$SwitchMap$com$zimbra$cs$mailbox$calendar$tzfixup$TimeZoneFixupRules$MatchBy[this.mMatchBy.ordinal()]) {
/*     */       case 1: 
/* 121 */         return true;
/*     */       case 2: 
/* 123 */         return this.mTZID.equalsIgnoreCase(tz.getID());
/*     */       case 3: 
/* 125 */         return (!tz.useDaylightTime()) && (tz.getRawOffset() == this.mStandardOffset);
/*     */       case 4: 
/*     */       case 5: 
/* 128 */         if (!tz.useDaylightTime())
/* 129 */           return false;
/* 130 */         if ((tz.getStandardOffset() != this.mStandardOffset) || (tz.getDaylightOffset() != this.mDaylightOffset))
/* 131 */           return false;
/* 132 */         if ((!sameOnset(this.mStandardOnset, tz.getStandardOnset())) || (!sameOnset(this.mDaylightOnset, tz.getDaylightOnset())))
/* 133 */           return false;
/* 134 */         return true;
/*     */       }
/* 136 */       return false;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public TimeZoneFixupRules(Element tzFixupElem)
/*     */     throws ServiceException
/*     */   {
/* 144 */     this.mMatchers = XmlFixupRules.parseTzFixup(tzFixupElem);
/*     */   }
/*     */   
/*     */   public TimeZoneFixupRules(Map<String, ICalTimeZone> replacements) {
/* 148 */     this.mMatchers = new ArrayList();
/* 149 */     for (Map.Entry<String, ICalTimeZone> entry : replacements.entrySet()) {
/* 150 */       String oldTZID = (String)entry.getKey();
/* 151 */       ICalTimeZone replacementTZ = (ICalTimeZone)entry.getValue();
/* 152 */       Matcher m = new Matcher(oldTZID, replacementTZ);
/* 153 */       this.mMatchers.add(m);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private ICalTimeZone fixTZ(ICalTimeZone oldTZ, Map<String, ICalTimeZone> replaced)
/*     */   {
/* 166 */     for (Matcher matcher : this.mMatchers) {
/* 167 */       if (matcher.matches(oldTZ)) {
/* 168 */         String oldID = oldTZ.getID();
/* 169 */         if (matcher.isTouchOnly()) {
/* 170 */           ZimbraLog.calendar.info("Touching timezone: " + oldID);
/*     */           
/* 172 */           replaced.put(oldID, oldTZ);
/* 173 */           return oldTZ;
/*     */         }
/* 175 */         ICalTimeZone newTZ = matcher.getReplacementTZ();
/* 176 */         ZimbraLog.calendar.info("Found replacement timezone: old=" + oldID + ", new=" + newTZ.getID());
/*     */         
/* 178 */         replaced.put(oldID, newTZ);
/* 179 */         return newTZ.cloneWithNewTZID(oldID);
/*     */       }
/*     */     }
/*     */     
/* 183 */     return null;
/*     */   }
/*     */   
/*     */   private int fixTZMap(TimeZoneMap tzmap, Map<String, ICalTimeZone> replaced)
/*     */   {
/* 188 */     int numFixed = 0;
/* 189 */     if (tzmap == null) return 0;
/* 190 */     List<ICalTimeZone> newTZList = new ArrayList();
/* 191 */     for (Iterator<ICalTimeZone> iter = tzmap.tzIterator(); iter.hasNext();) {
/* 192 */       ICalTimeZone tz = (ICalTimeZone)iter.next();
/* 193 */       ICalTimeZone newTZ = fixTZ(tz, replaced);
/* 194 */       if (newTZ != null) {
/* 195 */         iter.remove();
/* 196 */         newTZList.add(newTZ);
/*     */       }
/*     */     }
/* 199 */     for (ICalTimeZone newTZ : newTZList) {
/* 200 */       tzmap.add(newTZ);
/* 201 */       numFixed++;
/*     */     }
/* 203 */     return numFixed;
/*     */   }
/*     */   
/*     */   public int fixCalendarItem(CalendarItem calItem, Map<String, ICalTimeZone> replaced)
/*     */   {
/* 208 */     int numFixed = 0;
/* 209 */     TimeZoneMap tzmap = calItem.getTimeZoneMap();
/* 210 */     numFixed += fixTZMap(tzmap, replaced);
/* 211 */     Invite[] invites = calItem.getInvites();
/* 212 */     for (Invite inv : invites) {
/* 213 */       if (inv != null) {
/* 214 */         TimeZoneMap tzmapInv = inv.getTimeZoneMap();
/* 215 */         numFixed += fixTZMap(tzmapInv, replaced);
/*     */       }
/*     */     }
/* 218 */     return numFixed;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/calendar/tzfixup/TimeZoneFixupRules.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */